<template>
	<!-- 商品列表区域 -->
	<view class="goodsList">
		<!-- 商品列表主体 -->
		<view class="goodsList-item" v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
			<my-goods :item="item" :showRadio="false" :showNumber="false"></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 查询参数对象
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				// 商品列表数据
				goodsList:[],
				total:0,
				// 节流阀
				isLoading:true
				
			};
		},
		onLoad(options) {
			// console.log(options.cid,options.query)
			this.queryObj.query = options.query || ''
			this.queryObj.cid  = options.cid || ''
			this.getGoodsList()
		},
		methods:{
			// 获取商品列表数据
			async getGoodsList(cb) {
				this.isLoading = true
				const {data:data} = await uni.$http.get('public/v1/goods/search',this.queryObj)
				this.isLoading = false
				cb && cb()
				if(data.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...data.message.goods]
				this.total = data.message.total
				// console.log(data)
			},
			// 跳转至商品详情页
			gotoDetail(item) {
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
			
		},
		// 下拉触底事件
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			if(this.isLoading) return
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		// 上拉刷新事件
		onPullDownRefresh() {
			this.queryObj.pagenum = 1,
			this.total = 0,
			this.isLoading = false
			this.goodsList = []
			
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
 .goodsList{
	 display: flex;
	 flex-direction:column;
 }
 
</style>
